﻿@using System.Data;
@using GatewayServer.Common;
@using UtilsBase;
@{
    ViewBag.PageTitle = "洪峰智能";

    Layout = "~/Views/Shared/PageTemplate.cshtml";
}

<script>
    $(document).ready(function () {

        if ("@(ViewBag.order)" == "upd") {
            getEdit("@(ViewBag.m_guid)");
        }
        else {

            getAgreementList();
        }

        getEditList();


    });


    function getEdit(dguid) {
        $.post("getModel", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data);
            $("#m_name").val(myobj.m_name);
            $("#hid_m_type_guid").val(myobj.m_type_guid);

            $("#hid_dc_agreement_guid").val(myobj.dc_agreement_guid);
            $("#m_remark").val(myobj.m_remark);
            $("#m_parameter_demo").val(myobj.m_parameter_demo);
            $("#m_parameter_demo_init").val(myobj.m_parameter_demo_init);

            $("#m_command_explain").val(myobj.m_command_explain);
            for (var i = 0; i < document.getElementById("m_status").options.length; i++) {
                if (document.getElementById("m_status").options[i].value == myobj.m_status) {
                    document.getElementById("m_status").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("m_status").options[i].selected = false;

                }
            }

            getVariableList();
        });
    }

    function getAgreementList() {
        $("#dc_agreement_guid").empty();
        $("#dc_agreement_guid").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getAgreementList", { "flag": Math.random() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#dc_agreement_guid").prepend("<option value='" + myobj[i].a_guid + "'>" + myobj[i].a_name + "</option>")
            }
            for (var i = 0; i < document.getElementById("dc_agreement_guid").options.length; i++) {
                if (document.getElementById("dc_agreement_guid").options[i].value == $("#hid_dc_agreement_guid").val()) {
                    document.getElementById("dc_agreement_guid").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("dc_agreement_guid").options[i].selected = false;

                }
            }
        });
    }

    function getEditList() {
        $.post("getCommandByModel", { "flag": Math.random(), modelguid: $("#dc_model_guid").val(), type: "follow" }, function (result) {
            var pagedata = (result.data);
            rowcount = 0;
            var htmlstr = "";
            htmlstr = htmlstr + "<tr class='plainItemTRTitle'>";
            htmlstr = htmlstr + "<td >序号</td >";
            htmlstr = htmlstr + "<td >名称</td >";
            htmlstr = htmlstr + "<td >key</td >";
            htmlstr = htmlstr + "<td >内容</td >";
            htmlstr = htmlstr + "<td >状态</td >";
            htmlstr = htmlstr + "<td >协议</td >";
            htmlstr = htmlstr + "<td >类型</td >";
            htmlstr = htmlstr + "<td >操作</td >";
            htmlstr = htmlstr + "</tr >";
            document.getElementById("pagelist_follow").innerHTML = "";
            if (pagedata != null && pagedata.length > 0) {
                for (var i = 0; i < pagedata.length; i++) {
                    rowcount = i + 1;

                    var keytype="";
                    if (pagedata[i].dc_key_type==0)
                    {
                        keytype="一般指令";
                    }
                    else if (pagedata[i].dc_key_type==1)
                    {
                        keytype="设备初始化指令";
                    }
                   var status="";
                    if (pagedata[i].dc_status==0)
                    {
                        status="启用";
                    }
                    else if (pagedata[i].dc_status == 1) {
                        status = "禁用";
                    }
                    htmlstr = htmlstr + "<tr>";
                    htmlstr = htmlstr + "<td >" + rowcount + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].dc_name + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].dc_key + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].dc_content + "</td >";
                    htmlstr = htmlstr + "<td >" + status + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].dc_agreement_type + "</td >";
                    htmlstr = htmlstr + "<td >" + keytype + "</td >";
                    htmlstr = htmlstr + "<td ><a href=\"javascript:delcommand('" + pagedata[i].dc_guid + "')\">删除</a> </td >";
                    htmlstr = htmlstr + "</tr >";

                }
                document.getElementById("pagelist_follow").innerHTML = htmlstr;
            }
        });
    }
    function saveAll() {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("saveAllCommand", data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
               // window.location.href = "list";
                getEditList();
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }

    function delcommand(guid) {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("delcommand?guid=" + guid, data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                getEditList();

            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }


</script>

<style>
    #pagelist_follow {
        width: 100%;
    }

    #pagelist_main {
        width: 100%;
    }

    .kv-item label {
        width: 150px;
    }
</style>
<div id="saper-container">
    <form id="addForm" name="addForm" action="list" method="post">
        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>设备管理</h2>
            </div>
            <div class="subfiled-content">

                <div id="" title="添加/编辑">
                    <form action="" name="addForm" id="addForm" method="post">
                        <input type="hidden" name="dc_model_guid" id="dc_model_guid" value="@ViewBag.dc_model_guid" />
                        <input type="hidden" name="dc_guid" id="dc_guid" value="@ViewBag.dc_guid" />

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>名称：</label>
                            <div class="kv-item-content">
                                <input type="text" name="dc_name" id="dc_name" placeholder="名称" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>key：</label>
                            <div class="kv-item-content">
                                <input type="text" name="dc_key" id="dc_key" placeholder="key" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>指令内容：</label>
                            <div class="kv-item-content">
                                <input type="text" name="dc_content" id="dc_content" placeholder="指令内容" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>状态：</label>
                            <div class="kv-item-content">
                                <select id="dc_status" name="dc_status" style="width:130px;">
                                    <option value="0">启用</option>
                                    <option value="1">禁用</option>
                                </select>

                            </div>
                        </div>


                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>协议：</label>
                            <div class="kv-item-content">
                                <select id="dc_agreement_guid" name="dc_agreement_guid" style="width:130px;">
                                </select>
                                <input type="hidden" name="hid_dc_agreement_guid" id="hid_dc_agreement_guid" value="" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>指令类型：</label>
                            <div class="kv-item-content">
                                <select id="dc_key_type" name="dc_key_type" style="width:130px;">
                                    <option value="0">一般指令（可展示）</option>
                                    <option value="1">设备初始化指令（不展示）</option>
                                </select>

                            </div>
                        </div>

                        <div>
                            <hr />

                            <div>
                                <table id="pagelist_follow" class="plainItemTable"></table>
                            </div>

                        </div>


                        <br /><br />

                        <div style="margin:0 auto;width:150px;">

                            <input type="button" id="save" name="save" value="保存变量" onclick="javascript:saveAll()" />
                       &nbsp;&nbsp; <input type="button" id="but_back1" name="but_back1" value="返回" onclick="javascript: window.location.href = 'list'" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div id="saper-ft"></div>
    </form>
</div>

